from sqlalchemy.orm import Session
from . import models, schemas

def get_rule_keyword(db: Session, rule_keyword_id: int):
    return db.query(models.RuleKeyword).filter(models.RuleKeyword.Id == rule_keyword_id).first()

def get_rule_keywords(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.RuleKeyword).offset(skip).limit(limit).all()

def create_rule_keyword(db: Session, rule_keyword: schemas.RuleKeywordCreate):
    db_rule_keyword = models.RuleKeyword(**rule_keyword.dict())
    db.add(db_rule_keyword)
    db.commit()
    db.refresh(db_rule_keyword)
    return db_rule_keyword

def update_rule_keyword(db: Session, rule_keyword_id: int, rule_keyword: schemas.RuleKeywordCreate):
    db_rule_keyword = db.query(models.RuleKeyword).filter(models.RuleKeyword.Id == rule_keyword_id).first()
    if db_rule_keyword:
        for key, value in rule_keyword.dict().items():
            setattr(db_rule_keyword, key, value)
        db.commit()
        db.refresh(db_rule_keyword)
    return db_rule_keyword

def delete_rule_keyword(db: Session, rule_keyword_id: int):
    db_rule_keyword = db.query(models.RuleKeyword).filter(models.RuleKeyword.Id == rule_keyword_id).first()
    if db_rule_keyword:
        db.delete(db_rule_keyword)
        db.commit()
    return db_rule_keyword